import "../Room"
import QtQuick3D
import QtQuick
import MineCraftUtils
Node{
    id:root

    property Room target
    property int seed

    property MineCraftSceneBuilder builder:target?target.builder:null

    enum Style{
        Cave,//洞穴
        MineHole//矿坑
    }

    property int style:BaseBuilder.Cave

    property MineCraftRandomBrush randomBrush:MineCraftRandomBrush{
        id:randomBrush

        seed:12345
        threthold: 0.1
    }

    //绘制门
    function drawDoor(){
        builder.buildMode = MineCraftSceneBuilder.Override

        builder.brush.type = BlockMeta.OakPlank
        builder.drawBox(Qt.vector3d(-2,2,0),Qt.vector3d(4,1,1))
        builder.brush.type = BlockMeta.Fence1
        builder.drawBox(Qt.vector3d(-2,0,0),Qt.vector3d(1,2,1))
        builder.drawBox(Qt.vector3d(1,0,0),Qt.vector3d(1,2,1))
        builder.brush.type = BlockMeta.TorchOnWall
        builder.drawBlock(Qt.vector3d(-2,2,1))
        builder.drawBlock(Qt.vector3d(1,2,1))
        builder.brush.type = BlockMeta.None
        builder.buildMode = MineCraftSceneBuilder.Remove
        builder.drawBox(Qt.vector3d(-1,0,-1),Qt.vector3d(2,2,2))
    }

    //绘制单个方块
    function drawBlock(type,x,y,z){
        builder.push()
        builder.blockType = type
        builder.drawBlock(Qt.vector3d(x,y,z))
        builder.pop()
    }

    //绘制矿石
    function drawOre(seed,threshold,type){
        builder.buildMode = MineCraftSceneBuilder.Modify//绘图类型为修改模式
        builder.brush = randomBrush//使用随机画刷
        randomBrush.type = type
        randomBrush.threthold = threshold
        randomBrush.seed = seed
        builder.drawBox(Qt.vector3d(0,0,0),extend)
    }

    function buildCaveFrame(){
        builder.brush.type = BlockMeta.StoneBlock
        builder.drawBoxShell(Qt.vector3d(0,0,0),extend)//绘制地板

        builder.push()
        builder.buildMode = MineCraftSceneBuilder.Modify
        builder.brush.type = BlockMeta.DirtBlock
        builder.drawBox(Qt.vector3d(0,3,0),Qt.vector3d(16,3,16))
        builder.pop()

        builder.push()
        drawOre(Math.random(),0.025,BlockMeta.IronOre)//绘制矿石
        drawOre(Math.random(),0.05,BlockMeta.CoalOre)//绘制矿石
        builder.pop()
    }

    function buildMineHoleFrame(){

    }

    function buildFrame(){
        if(style === BaseBuilder.Cave)
        {
            buildCaveFrame()
        }
        else if(style === BaseBuilder.MineHole)
        {
            buildMineHoleFrame()
        }
    }

    Connections{
        target:root.target

        function onBuildScene(){
            buildFrame()//绘制整体框架

            //绘制出入口
            builder.push()
            builder.translate(Qt.vector3d(extend.x / 2,1,1))
            drawDoor()
            builder.reset()
            builder.translate(Qt.vector3d(extend.x / 2 - 1,1,14))
            builder.blockRotation = Qt.vector3d(0,180,0)
            builder.rotate(Qt.vector3d(0,180,0))
            drawDoor()
            builder.pop()
        }

    }
}
